/**

 @Name：用户管理
 @Author：xuqipeter
 @Site：http://blog.xuziyu.cn/
    
 */

layui.define(['table', 'layer', 'm-utils', 'form', 'admin', 'view'], function (exports) {
  var $ = layui.$,
    table = layui.table,
    layer = layui.layer,
    admin = layui.admin,
    view = layui.view,
    form = layui.form,
    setter = layui.setter,
    baseUrl = setter.baseUrl,
    ossUrl = setter.ossUrl,
    utils = layui['m-utils'];
  var UserModel = {
    demo() {
      console.log('demo')
    },
    layerFull: null,
    tableList: null,
    /* 获取用户管理列表 */
    _tableList() {
      return table.render({
        elem: '#tableList',
        url: `${baseUrl}admin/user/getUserList`,
        toolbar: '#theadToolbar',
        parseData: function (res) {
          return utils.setParseData({
            res,
            data: 'userList'
          })
        },
        cols: [
          [{
              field: 'user_id',
              title: '用户ID'
            },
            {
              field: 'number',
              title: '用户编号'
            },
            {
              field: 'is_agent',
              title: '是否董事',
              templet: function (d) {
                let text = '否'
                if (d.parent_number && d.is_agent != 1) {
                  text = `<a class="layui-btn layui-btn-xs" lay-event="createAgentOrder">开通董事</a>`
                }
                if (d.is_agent == 1) {
                  text = '是'
                }
                return text
              }
            },
            {
              field: 'is_founder',
              title: '是否联创',
              templet: function (d) {
                let text = '否'
                if (d.is_founder == 1) {
                  text = '是'
                }
                return text
              }
            },
            {
              field: 'is_area_agent',
              title: '是否区代',
              width: 180,
              templet: function (d) {
                let text = '否'
                if (d.parent_number && d.is_area_agent != 1) {
                  text = `<a class="layui-btn layui-btn-xs" lay-event="createAreaAgentOrder">开通区代</a>`
                }
                if (d.is_area_agent == 1) {
                  text = d.areas || '是'
                }
                return text
              }
            },
            {
              field: 'realname',
              title: '姓名'
            },
            {
              field: 'mobile',
              title: '手机'
            },
            {
              field: 'receiver_area_name',
              title: '收货人省市区',
              width: 200
            },
            {
              field: 'receiver_address',
              title: '收货人地址',
              width: 200
            },
            {
              field: 'id_card',
              title: '身份证号'
            },
            {
              field: 'bank',
              title: '银行名'
            },
            {
              field: 'bankcard',
              title: '银行卡号',
              width: 190
            },
            {
              field: 'parent_number',
              title: '上级编号'
            },
            {
              field: 'balance',
              title: '余额（元）'
            },
            {
              field: 'block_balance',
              title: '冻结金额（元）',
              width: 140
            },
            {
              field: 'created_time',
              title: '创建时间',
              width: 180,
              templet: function (d) {
                if (!d.created_time) return ''
                return utils.formatTime(d.created_time)
              }
            },
            {
              field: 'parent_number',
              title: '上级编号',
            },
            {
              title: '直接开通',
              // fixed: 'right',
              width: 220,
              align: 'center',
              toolbar: '#trToolbarbarSide'
            },
            {
              title: '操作',
              fixed: 'right',
              width: 160,
              align: 'center',
              toolbar: '#trToolbar'
            }
          ]
        ],
        done: function (d) {
          console.log(d)
          console.log(setter)
          if (d[setter.response.statusName] == setter.response.statusCode.logout) {
            view.exit()
          }
        }
      })
    },
    /* 创建用户 */
    _createUser({
      fieldData,
      callback
    }) {
      let {
        mobile,
        password,
        realname,
        bankcard,
        bank,
        receiver_address,
        receiver_area_name
      } = fieldData;
      let that = this;
      admin.req({
        url: `${baseUrl}admin/user/createUser`,
        method: 'post',
        data: {
          mobile,
          password,
          realname,
          bankcard,
          bank,
          receiver_address,
          receiver_area_name,
        },
        dataType: 'json',
        done: function (res) {
          utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          })
          callback && callback(res)
        }
      })
    },
    /* 开通用户权限 */
    _openUser({
      fieldData,
      callback
    }) {
      let {
        type,
        user_id,
        area_name = ''
      } = fieldData
      let that = this;
      admin.req({
        url: `${baseUrl}admin/user/openUser`,
        method: 'post',
        data: {
          type,
          user_id,
          area_name,
        },
        dataType: 'json',
        done: function (res) {
          utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          })
          callback && callback(res)
        }
      })
    },
    /* 开通总代 */
    _createAgentOrder({
      fieldData,
      callback
    }) {
      let {
        user_id,
        area_name,
        address,
        mobile,
        amount,
        count,
        receiver_name
      } = fieldData
      let that = this;
      admin.req({
        url: `${baseUrl}admin/order/createAgentOrder`,
        method: 'post',
        data: {
          user_id,
          area_name,
          address,
          mobile,
          amount,
          count,
          receiver_name
        },
        dataType: 'json',
        done: function (res) {
          /* utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          }) */
          callback && callback(res)
        }
      })
    },
    /* 开通区代 */
    _createAreaAgentOrder({
      fieldData,
      callback
    }) {
      let {
        user_id,
        area_name,
        address,
        mobile,
        count,
        amount,
        receiver_name
      } = fieldData
      let that = this;
      admin.req({
        url: `${baseUrl}admin/order/createAreaAgentOrder`,
        method: 'post',
        data: {
          user_id,
          area_name,
          address,
          mobile,
          count,
          amount,
          receiver_name
        },
        dataType: 'json',
        done: function (res) {
          /* utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          }) */
          callback && callback(res)
        }
      })
    },
    _bindNumber({
      fieldData,
      callback
    }) {
      let {
        number,
        user_id
      } = fieldData
      let that = this
      admin.req({
        url: `${baseUrl}admin/user/bindNumber`,
        method: 'post',
        data: {
          number,
          user_id
        },
        dataType: 'json',
        done: function (res) {
          utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          })
          callback && callback(res)
        }
      })
    },
    _edit() {

    },
    _del() {

    }
  }

  exports('UserModel', UserModel)
})